<template>
	<view class="container home-bg home-pd home-h flex flex-col">
		<customInput @confirm="confirm"   />
		<uv-tabs @change="tabsChange" :list="tabsList"></uv-tabs>
		<view class="taskList-container flex-1">
			<taskListItem  :tabsList="tabsList" :item="item" v-for="(item,index) in list"   />
		</view>
	</view>
</template>

<script setup>
	import customInput from '../components/custom-input';
	import taskListItem from '../components/task-list-item';
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh,
		onShow
	} from '@dcloudio/uni-app';

	import {
		getTaskList
	} from '@/api/task.js';
	const tabsList = ref([{		name: '全部',
		value:'',
		className:""	}, {		name: '等待入库',
		value:0,
		className:"tag-danger"	}, {		name: '入库成功',
		value:1,
		className:"tag-success"	}, {		name: '拒绝入库',
		value:2,
		className:"tag-error"	}, {		name: '结束入库',
		value:3,
		className:""	}]); 
	let params = reactive({		taskStatus: "", 		pageNo: 1,		pageSize: 10,		productListName:""	}); 
	let list = ref([]); 
	onShow(()=>{
			 if(list.value.length){
				 setList({
					 ...params,
					 pageNo:1,
					 pageSize:list.value.length
				 });
			 }
	});
	onLoad(() => {
		init()
	});
	onReachBottom(async () => {
		params.pageNo += 1;
		setList()
	})
	onPullDownRefresh(() => {
		params.pageNo = 1;
		init()
	});  
	async function init() { 
		setList()
	}
	async function tabsChange(e){
		params.taskStatus = e.value; 
		params.pageNo = 1;
		setList()
	}
	async function confirm(e) { 
		params.productListName = e;
		params.pageNo = 1;
		setList()
	}
	async function setList() {
		list.value = await getList();
	} 
	function getList(requestData = params) {
		return new Promise((re, rj) => {  
			getTaskList(requestData).then(res => { 
				uni.stopPullDownRefresh();
				if (!res.data.list) {
					uni.showToast({
						title: '没有数据了',
						icon: 'none'
					})
				}
				let {
					data = {
						list: []
					}
				} = res;
				re(requestData.pageNo != 1 ? list.value.concat(data.list) : data.list);
			});
		})

	} 
</script>

<style lang="scss" scoped>
	@import 'index.scss';
</style>